package disjunction.examples.entities
{
	import disjunction.Controller;
	import disjunction.examples.entities.DemoEntityModel;
	import disjunction.examples.entities.DemoModel;
	import disjunction.extensions.math.MathUtils;
	import disjunction.extensions.math.Vector2;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	/**
	 * Entities Example controller.
	 * 
	 * @author Nick Wiggill
	 */
	
	public class DemoController extends Controller
	{		
		public override function initialise(data:Object):void
		{
			//set up the various entities.
			var entity:DemoEntityModel;
			var numEntities:Number = 
				Math.random() * (model_.MAX_PEOPLE - model_.MIN_PEOPLE) + model_.MIN_PEOPLE;
			
			for (var i:int = 0; i < numEntities; i++)
			{
				entity = new DemoEntityModel();
				entity.radius = Math.random() * 5 + 5;
				entity.position.x = Math.random() * (view.context as DisplayObject).stage.stageWidth;//550;
				entity.position.y = Math.random() * (view.context as DisplayObject).stage.stageHeight;//400;
				entity.motion.x = Math.random() * 200 - 100;
				entity.motion.y = Math.random() * 200 - 100;
				entity.type = int(Math.random() * 4); //num types!

				model_.toAdd.push(entity);
			}
			
			view.start();
		}
		
		protected override function update(timeDeltaMs:uint):void
		{
			var rotation:Number;
			var context:DisplayObject = view.context as DisplayObjectContainer;
			
			for each (var entity:DemoEntityModel in model.current)
			{
				switch (entity.type)
				{
					case DemoEntityModel.TYPE_BOUNCER:
						//apply motion in pixels per second
						entity.position.addLocal(entity.motion.mul(timeDeltaMs/1000));
						
						//bounce if edge hit
						
						trace('context is', context);
						if (entity.position.x < entity.radius ||
							entity.position.x > context.stage.stageWidth - entity.radius)
							entity.motion.x *= -1;
						
						if (entity.position.y < entity.radius ||
							entity.position.y > context.stage.stageHeight - entity.radius)
							entity.motion.y *= -1;
						break;
					
					case DemoEntityModel.TYPE_DANCER:
						//rotate motion vector by some random value
						entity.motion.rotateLocal(Math.random()*Math.PI/18 - Math.PI/9);
						//apply motion in pixels per second
						entity.position.addLocal(entity.motion.mul(timeDeltaMs/1000));
						break;
					
					case DemoEntityModel.TYPE_WANDERER:
						//rotate motion vector by some random value
						rotation = (Math.random() * .6) - .3;
						entity.motion.rotateLocal(rotation);
						//apply motion in pixels per second
						entity.position.addLocal(entity.motion.mul(timeDeltaMs/1000));
						//keep in bounds
						entity.position.x = MathUtils.clamp(entity.position.x, 0, context.stage.stageWidth);
						entity.position.y = MathUtils.clamp(entity.position.y, 0, context.stage.stageHeight);
						break;
					
					case DemoEntityModel.TYPE_JITTERER:
						//rotate motion vector by some random value
						rotation = (Math.random() * 20) - 10;
						entity.motion.rotateLocal(rotation);
						//apply motion in pixels per second
						entity.position.addLocal(entity.motion.mul(timeDeltaMs/1000));
						//keep in bounds
						entity.position.x = MathUtils.clamp(entity.position.x, 0, context.stage.stageWidth);
						entity.position.y = MathUtils.clamp(entity.position.y, 0, context.stage.stageHeight);
						break;
					
				}
				
			}
			
			super.update(timeDeltaMs);
		}
		
		public function get model_():DemoModel
		{
			return model as DemoModel;
		}
	}
}